package com.swgk.core.view.areapopup;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.swgk.core.dialog.BaseAreaPopWindow;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 三级筛选地区弹框
 */

public class ThreeLevelAreaWindow extends BaseAreaPopWindow {

    private List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> baseAreaEntityList;
    // 一级联动数据
    private final List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> mProvinceList = new ArrayList<>();
    // 二级联动数据
    private final List<List<BaseAreaEntity<List<BaseAreaEntity>>>> mCityList = new ArrayList<>();
    // 三级联动数据
    private final List<List<List<BaseAreaEntity>>> mDistrictList = new ArrayList<>();
    boolean isAddProFrirstData = false;

    private Gson gson;

    public ThreeLevelAreaWindow(Context context) {
        super(context);
        gson = new Gson();
    }


    @Override
    protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
    }

    /**
     * 获取选择的文字
     *
     * @param name
     * @param simpleName
     */
    @Override
    protected void resultContent(String name, String simpleName) {

    }

    @Override
    protected void cityMoreClick(List list, int position) {
    }

    @Override
    protected void provinceMoreClick(List list, int position) {
    }


    @Override
    protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
    }

    @Override
    protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
    }

    @Override
    protected void areaMoreClick(List list, int position) {
    }

    /**
     * 设置数据，默认省市单选
     */
    public void setData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> listDatas, boolean isAddFirstData) {

        String cityListsStr = gson.toJson(listDatas);
        List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = gson.fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
        }.getType());
        this.baseAreaEntityList = list;
        if (isAddFirstData) {
            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("不限");
            baseAreaEntity.setNo("");
            baseAreaEntity.setSimpleName("不限");
            list.add(0, baseAreaEntity);
            for (int i = 0; i < list.size(); i++) {
                if (i != 0) {
                    list.get(i).getChildren().add(0, baseAreaEntity);
                    for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                        list.get(i).getChildren().get(j).getChildren().add(0, baseAreaEntity);
                    }
                }
            }
        }
        super.BaseData(list);
    }

    /**
     * 设置数据和相关位置
     *
     * @param listDatas
     * @param provinceName
     * @param cityName
     * @param areaName
     * @param isAddFirstData     是否都添加不限
     * @param isAddProFrirstData 是否省份添加不限
     */
    public void setDataAndPosition(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> listDatas, String provinceName, String cityName, String areaName, boolean isAddFirstData, boolean isAddProFrirstData) {
        this.isAddProFrirstData = isAddProFrirstData;
        String cityListsStr = gson.toJson(listDatas);
        List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = gson.fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
        }.getType());
        BaseAreaEntity[] areaEntities;
        this.baseAreaEntityList = list;
        if (isAddFirstData) {
            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("不限");
            baseAreaEntity.setNo("");
            baseAreaEntity.setSimpleName("不限");
            if (isAddProFrirstData) {
                list.add(0, baseAreaEntity);
            }
            for (int i = 0; i < list.size(); i++) {
                if (i == 0 && isAddProFrirstData) {
                    continue;
                }
                list.get(i).getChildren().add(0, baseAreaEntity);
                for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                    list.get(i).getChildren().get(j).getChildren().add(0, baseAreaEntity);
                }
            }
            for (BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>> datas : list) {
                mProvinceList.add(datas);

                mCityList.add(datas.getChildren());
                ArrayList areaList = new ArrayList<>();
                if (datas.getChildren() == null) {
                    continue;
                }
                for (BaseAreaEntity<List<BaseAreaEntity>> area : datas.getChildren()) {
                    if (area.getChildren() == null) {
                        area.setChildren(new ArrayList<>());
                    }
                    areaList.add(area.getChildren());
                }
                mDistrictList.add(areaList);
            }

        }
        areaEntities = findCode(provinceName, cityName, areaName);
        if (areaEntities[0] == null && areaEntities[1] == null && areaEntities[2] == null) {
            super.BaseDataAndPosition(list, 0, 0, 0);
            return;
        }
        super.BaseDataAndPosition(list, areaEntities[0].getIndex(), areaEntities[1].getIndex(), areaEntities[2].getIndex());
    }


    /**
     * 设置省单选
     */
    public void setProvinceSingle() {
        super.setProvinceType(false);
    }

    /**
     * 设置省多选
     */
    public void setProvinceMore() {
        super.setProvinceType(true);
    }

    /**
     * 设置市单选
     */
    public void setCitySingle() {
        super.setCityType(false);
    }

    /**
     * 设置市多选
     */
    public void setCityMore() {
        super.setCityType(true);
    }

    /**
     * 设置区单选
     */
    public void setAreaSingle() {
        super.setAreaType(false);
    }

    /**
     * 设置区多选
     */
    public void setAreaMore() {
        super.setAreaType(true);
    }

    public void setFirstListIsGone(boolean isGone) {
        super.setFirstListIsGone(isGone);
    }

    public void setSecondaryListIsGone(boolean isGone) {
        super.setSecondaryListIsGone(isGone);
    }

    public void setThreeListIsGone(boolean isGone) {
        super.setThreeListIsGone(isGone);
    }

    public void setLayoutPadding(int left, int right) {
        super.setLayoutPadding(left, right);
    }

    /**
     * 查找省市区编码
     *
     * @param provinceStr 省名称
     * @param cityStr     市名称
     * @param districtStr 区名称
     * @return 长度为3的省市区地址对象数组，0：省，1：市，2：区，都可能为null
     */
    public BaseAreaEntity[] findCode(String provinceStr, String cityStr, String districtStr) {
        //省市区弹窗选中位置
        BaseAreaEntity[] code = new BaseAreaEntity[3];
        //防止内容为null
        provinceStr = DefaultUtil.getString(provinceStr);
        cityStr = DefaultUtil.getString(cityStr);
        districtStr = DefaultUtil.getString(districtStr);
        //
        String selectProvince = provinceStr;
        //针对特殊省处理
        if (!TextUtils.isEmpty(selectProvince)) {
            if (selectProvince.contains("台湾")) {
                //针对特殊省处理
                selectProvince = "台湾";
            } else if (selectProvince.endsWith("市")) {
                //针对直辖市处理(适应接口)
                selectProvince = selectProvince.substring(0, selectProvince.length() - 1);
            }
        }
        //查找省position
        for (BaseAreaEntity entity : mProvinceList) {
            if (entity.getName().contains(selectProvince)) {
                //设置省编码
                entity.setIndex(mProvinceList.indexOf(entity));
                code[0] = entity;
                break;
            }
        }
        if(code[0] != null && mCityList.get(code[0].getIndex())==null){
            BaseAreaEntity entity = new BaseAreaEntity();
            entity.setIndex(0);
            code[1] = entity;
            code[2] = entity;
            return code;
        }
        //查找市position
        if (code[0] != null && code[0].getIndex() < mCityList.size()) {
            List<BaseAreaEntity<List<BaseAreaEntity>>> cityList = mCityList.get(code[0].getIndex());
            for (BaseAreaEntity entity : cityList) {
                if (entity.getName().contains(cityStr)) {
                    //设置城市编码
                    entity.setIndex(cityList.indexOf(entity));
                    code[1] = entity;
                    break;
                }
            }
        }
        //查找区position
        if (code[0] != null && code[1] != null
                && code[0].getIndex() < mDistrictList.size()
                && code[1].getIndex() < mDistrictList.get(code[0].getIndex()).size()) {
            if (districtStr.endsWith("市") || districtStr.endsWith("县") || districtStr.endsWith("区")) {
                districtStr = districtStr.substring(0, districtStr.length() - 1);
            }
            List<BaseAreaEntity> districtList = null;
            if (!isAddProFrirstData) {
                districtList = mDistrictList.get(code[0].getIndex()).get(code[1].getIndex());
            } else {
                districtList = mDistrictList.get(code[0].getIndex() - 1).get(code[1].getIndex());
            }
            if (districtList.size() == 0) {
                BaseAreaEntity entity = new BaseAreaEntity();
                entity.setIndex(0);
                code[2] = entity;
            } else {
                for (BaseAreaEntity entity : districtList) {
                    if (entity != null) {
                        if (entity.getName().contains(districtStr)) {
                            //设置区域编码
                            entity.setIndex(districtList.indexOf(entity));
                            code[2] = entity;
                            break;
                        }
                    }
                }
            }

        } else {
            BaseAreaEntity entity = new BaseAreaEntity();
            entity.setIndex(0);
            code[2] = entity;
        }
        return code;
    }

    /**
     * 全部单选回调代理，兼容组装数据
     */
    public static abstract class OnResultClickProxy implements OnResultClick {
        @Override
        public void result(Object proId, Object cityId, Object areaId) {
            BaseAreaEntity pro = (BaseAreaEntity) proId;
            BaseAreaEntity city = (BaseAreaEntity) cityId;
            BaseAreaEntity area = (BaseAreaEntity) areaId;
            GoodsSourceLocationEntity entity = new GoodsSourceLocationEntity();
            if (pro != null) {
                entity.setProvinceName(pro.getName());
                entity.setProvinceNo(pro.getNo());
                entity.setProvinceSimpleName(pro.getSimpleName());
            }
            if (city != null) {
                entity.setCityNo(city.getNo());
                entity.setCityName(city.getName());
                entity.setCitySimpleName(city.getSimpleName());
            }
            if (area != null) {
                entity.setAreaName(area.getName());
                entity.setAreaNo(area.getNo());
            }
            result(entity);
        }

        protected abstract void result(GoodsSourceLocationEntity entity);
    }


    public void setDataAndPosition(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> listDatas, String provinceName, String cityName, String areaName, boolean isAddFirstData, boolean isAddProFrirstData, String areaCode) {
        this.isAddProFrirstData = isAddProFrirstData;
        String cityListsStr = gson.toJson(listDatas);
        List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = gson.fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
        }.getType());
        BaseAreaEntity[] areaEntities;
        this.baseAreaEntityList = list;
        if (isAddFirstData) {
            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("不限");
            baseAreaEntity.setNo("");
            baseAreaEntity.setSimpleName("不限");
            if (isAddProFrirstData) {
                list.add(0, baseAreaEntity);
            }
            for (int i = 0; i < list.size(); i++) {
                if (i == 0 && isAddProFrirstData) {
                    continue;
                }
                list.get(i).getChildren().add(0, baseAreaEntity);
                for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                    list.get(i).getChildren().get(j).getChildren().add(0, baseAreaEntity);
                }

                if (list.get(i).getId().equals(areaCode)) {
                    setDefaultByAreaCode(i);
                }
            }
            for (BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>> datas : list) {
                mProvinceList.add(datas);

                mCityList.add(datas.getChildren());
                ArrayList areaList = new ArrayList<>();
                if (datas.getChildren() == null) {
                    continue;
                }
                for (BaseAreaEntity<List<BaseAreaEntity>> area : datas.getChildren()) {
                    if (area.getChildren() == null) {
                        area.setChildren(new ArrayList<>());
                    }
                    areaList.add(area.getChildren());
                }
                mDistrictList.add(areaList);
            }

        }
        areaEntities = findCode(provinceName, cityName, areaName);
        if (areaEntities[0] == null && areaEntities[1] == null && areaEntities[2] == null) {
            super.BaseDataAndPosition(list, 0, 0, 0);
            return;
        }
        super.BaseDataAndPosition(list, areaEntities[0].getIndex(), areaEntities[1].getIndex(), areaEntities[2].getIndex());
    }

    public void setData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> listDatas, boolean isAddFirstData, String areaCode) {
        int position = 0;
        String cityListsStr = gson.toJson(listDatas);
        List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = gson.fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
        }.getType());
        this.baseAreaEntityList = list;
        if (isAddFirstData) {
            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("不限");
            baseAreaEntity.setNo("");
            baseAreaEntity.setId("");
            baseAreaEntity.setSimpleName("不限");
            list.add(0, baseAreaEntity);
            for (int i = 0; i < list.size(); i++) {
                if (i != 0) {
                    list.get(i).getChildren().add(0, baseAreaEntity);
                    for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                        list.get(i).getChildren().get(j).getChildren().add(0, baseAreaEntity);
                    }
                }
                if (list.get(i).getId().equals(areaCode)) {
//                    setDefaultByAreaCode(i);
                    position = i;
                }
            }
        }
        super.BaseData(list);
        setDefaultForPosition(position);
    }

    public void setDataForCompany(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> listDatas, boolean isAddFirstData) {
        String cityListsStr = gson.toJson(listDatas);
        List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = gson.fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
        }.getType());
        this.baseAreaEntityList = list;
        if (isAddFirstData) {
            BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
            baseAreaEntity.setName("不限");
            baseAreaEntity.setNo("");
            baseAreaEntity.setId("");
            baseAreaEntity.setSimpleName("不限");
            list.add(0, baseAreaEntity);
            for (int i = 0; i < list.size(); i++) {
                if (i != 0) {
                    list.get(i).getChildren().add(0, baseAreaEntity);
                    for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                        list.get(i).getChildren().get(j).getChildren().add(0, baseAreaEntity);
                    }
                }
            }
        }
        super.BaseData(list);
    }
}
